﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
namespace SchoolManagementSystem.Details
{
    public partial class Student_ResultUC : DetailUC
    {
        public Student_ResultUC()
        {
            InitializeComponent();
            this.ObjectName = "điểm học viên";
            this.Code = "Student_Point";
            this.Type = new Student();
            commandBarUC1.cmdnew.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            txtbranch.Enabled = false;
            txtcode.Enabled = false;
           // txtgvcn.Enabled = false;
            txtkhoa.Enabled = false;
            txtlop.Enabled = false;
           // txtmonhoc.Enabled = false;
            txtname.Enabled = false;

            masterMainUC1.Model = RegisterService.RegisterServices.StudentResultDetailService;
            masterMainUC1.DetailUCControl = new Student_ResultDetailUC();
        }

        String StudentID = "1=1";
        protected override long DetailDisplay()
        {
            commandBarUC1.cmdnew.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            Student data = new Student();
            if (ID > 0)
            {
                data = (Student)RegisterService.RegisterServices.StudentService.GetByID(ID);
                StudentID = data.ID.ToString();
            }

            txtbranch.Text = data.BranchExt;
            txtcode.Text = data.Code;
          
            txtkhoa.Text = data.CourseExt;
            txtlop.Text = data.ClassExt;
            txtname.Text = data.Name;

            var Student_rating = (List<Student_Rating>)RegisterService.RegisterServices.StudentRatingService.GetList(" StudentID=" + ID);
            if (Student_rating != null && Student_rating.Count!=0)
            {
                cbxeploa.SelectedIndex = Student_rating[0].Rating;
                txtpoint.Text = Student_rating[0].TotalPoint.ToString();
            }
            else
            {
                cbxeploa.SelectedIndex = 0;
            }
            if (data.Active == true)
            {
                cbxeploa.Enabled = false;
                txtpoint.Enabled = false;
            }
            else
            {
                cbxeploa.Enabled = true;
                txtpoint.Enabled = true;
            }
            masterMainUC1.SearchString = " StudentID='" + StudentID + "'";
            masterMainUC1.DetailUCControl.SearchString = " StudentID='" + StudentID + "'";
            return ID;

        }

        protected override long Save()
        {
            commandBarUC1.cmdnew.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
           // commandBarUC1.cm.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            Student_Result data = new Student_Result();
            if (ID > 0)
            {
                data = (Student_Result)RegisterService.RegisterServices.StudentResultService.GetByStudentID(ID);
            }

            var Student_rating = (List<Student_Rating>)RegisterService.RegisterServices.StudentRatingService.GetList(" StudentID=" + ID);
            if (Student_rating != null && Student_rating.Count != 0)
            {
                var st = Student_rating[0];
                st.Rating = cbxeploa.SelectedIndex;
                try
                {
                    st.TotalPoint = float.Parse(txtpoint.Text);
                }
                catch {
                    MessageControl.ShowMessage("Điểm trung bình sai định dạng", "Warning", MessageControl.Icon.Warning);
                    return ID;
                }
                RegisterService.RegisterServices.StudentRatingService.Save(st);
            }
            else
            {
                var st = new Student_Rating();
                st.Rating = cbxeploa.SelectedIndex;
                try
                {
                    st.TotalPoint = float.Parse(txtpoint.Text);
                }
                catch
                {
                    MessageControl.ShowMessage("Điểm trung bình sai định dạng", "Warning", MessageControl.Icon.Warning);
                    return ID;
                }
                st.StudentID = ID;
                RegisterService.RegisterServices.StudentRatingService.Save(st);
            }
            masterMainUC1.DetailUCControl.SearchString = ID.ToString();
            return ID;
        }


        public override bool Delete(long SelectedID)
        {

            return RegisterService.RegisterServices.StudentResultService.Delete(RegisterService.RegisterServices.StudentResultService.GetByID(SelectedID));

        }

        private void Student_PointUC_Load(object sender, EventArgs e)
        {
            AddTextChanged(xtraTabPage1.Controls);
        } 


    }
}
